﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI.WebControls;
using Exhibition.Common.Web;

namespace Exhibition.Common.Helper
{
    public class FileUploadHelper
    {
        private string extensionLim = "";
        private int fileLengthLim = 0;
        private string filePath = "";
        private string fileSaveMethod = "R";
        private bool haveLoad;
        private string message = "";
        private HttpPostedFile myFile = null;
        private string savePath = "images";
        private string picName;

        public bool HTMLUpLoad()
        {
            string fileName = "";
            string str2 = "";
            if (this.myFile.FileName.Length > 0)
            {
                fileName = this.myFile.FileName;
                str2 = fileName.Substring(fileName.LastIndexOf('.'));
                if (this.extensionLim.IndexOf(str2.ToLower()) < 0)
                {
                    this.message = "不能上传该类型文件，您只能上传以下文件：" + this.extensionLim;
                    return false;
                }
                //if ((this.myFile.ContentLength / 0x400) > this.fileLengthLim)
                //{
                //    this.message = "上传的文件大于最大限制：" + this.fileLengthLim + "KB";
                //    return false;
                //}
                if (this.fileSaveMethod.Equals("R"))
                {
                    fileName = StringUtils.GetRandomNumber() + "_" + StringUtils.GetRandWord(10) + str2;
                }
                string path = StringUtils.GetWebConfigValue("ImgLocal") + this.savePath + @"\";
                if (!Directory.Exists(path))
                {
                    Directory.CreateDirectory(path);
                }
                this.myFile.SaveAs(path + fileName);
                if (this.savePath.Substring(0, 1) == "/")
                {
                    this.savePath = this.savePath.Substring(1, this.savePath.Length - 1);
                }
                this.filePath = this.savePath + "/" + fileName;
                this.haveLoad = true;
            }
            else
            {
                this.haveLoad = false;
            }
            return true;
        }

        public bool Upload()
        {
            string fileName = "";
            string str2 = "";
            fileName = this.myFile.FileName;
            str2 = fileName.Substring(fileName.LastIndexOf('.'));
            //if (this.extensionLim.IndexOf(str2.ToLower()) < 0)
            //{
            //    this.message = "不能上传该类型文件，您只能上传以下文件：" + this.extensionLim;
            //    return false;
            //}
            picName = fileName.Remove(fileName.LastIndexOf('.'), 4);
            if (this.fileSaveMethod.Equals("R"))
            {
                fileName = StringUtils.GetRandomNumber() + "_" + StringUtils.GetRandWord(10) + str2;
            }
            else
            {
                fileName = fileName.Substring(fileName.LastIndexOf(@"\"));
            }
            string path = StringUtils.GetWebConfigValue("ImgLocal") + DateTime.Now.ToString("yyyyMMdd") + "/";

            if (!Directory.Exists(HttpContext.Current.Server.MapPath(path)))
            {
                Directory.CreateDirectory(HttpContext.Current.Server.MapPath(path));
            }
            this.myFile.SaveAs(HttpContext.Current.Server.MapPath(path) + fileName);
            if (this.savePath.Substring(0, 1) == "/")
            {
                this.savePath = this.savePath.Substring(1, this.savePath.Length - 1);
            }
            this.filePath = StringUtils.GetWebConfigValue("ImgLocal") + DateTime.Now.ToString("yyyyMMdd") + "/" + fileName;
            this.haveLoad = true;

            return true;
        }

        public string ExtensionLim
        {
            get
            {
                return this.extensionLim;
            }
            set
            {
                this.extensionLim = value;
            }
        }

        public int FileLengthLim
        {
            get
            {
                return this.fileLengthLim;
            }
            set
            {
                this.fileLengthLim = value;
            }
        }

        public string FilePath
        {
            get
            {
                return this.filePath;
            }
            set
            {
                this.filePath = value;
            }
        }

        public string FileSaveMethod
        {
            get
            {
                return this.fileSaveMethod;
            }
            set
            {
                this.fileSaveMethod = value;
            }
        }

        public bool HaveLoad
        {
            get
            {
                return this.haveLoad;
            }
            set
            {
                this.haveLoad = value;
            }
        }

        public string Message
        {
            get
            {
                return this.message;
            }
        }

        public HttpPostedFile MyFile
        {
            get
            {
                return this.myFile;
            }
            set
            {
                this.myFile = value;
            }
        }



        public string SavePath
        {
            get
            {
                return this.savePath;
            }
            set
            {
                this.savePath = value;
            }
        }

        public string PicName { get { return this.picName; } set { this.picName = value; } }
    }
}
